package com.py.custom.business.demo.protocol.valid;

import com.py.custom.business.demo.protocol.request.StudentRequest;
import com.py.custom.common.web.valid.CustomValidHandler;
import com.py.custom.common.web.valid.annotation.CustomValid;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;

@Component
public class DateValid implements CustomValidHandler<StudentRequest> {

    @Override
    public boolean valid(CustomValid customValid, StudentRequest studentRequest) {
        // 出生时间戳
        Long date = studentRequest.getDate();
        Instant instant = Instant.ofEpochMilli(date);
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        LocalDate localDate = localDateTime.toLocalDate();

        // 当前时间
        LocalDate now = LocalDate.now();

        // 相差多少年
        int years = now.until(localDate).getYears();

        // 年龄
        Integer age = studentRequest.getAge();

        // 校验是否成功
        return age <= years;
    }
}
